public class PushUtil {
    private static String appId = "";
    private static String appKey = "";
    private static String masterSecret = "";
    private static String url = "http://sdk.open.api.igexin.com/apiex.htm";

    /**
     * 执行单推
     */
    public static void pushMessageToSingle(AppPush appPush, String cid) {
        Gson gson = new Gson();
        String s = gson.toJson(appPush);
        IGtPush push = new IGtPush(url, appKey, masterSecret);
        TransmissionTemplate template = new TransmissionTemplate();
        template.setAppId(appId);
        template.setAppkey(appKey);
        APNPayload apnPayload = getAPNPayload("+1", 1, "default", appPush);
        template.setAPNInfo(apnPayload); //ios消息推送
        template.setTransmissionContent(s);
        SingleMessage message = new SingleMessage();
        message.setOffline(true);
        // 离线有效时间，单位为毫秒
        message.setOfflineExpireTime(24 * 3600 * 1000);
        message.setData(template);
        Target target = new Target();
        target.setAppId(appId);
        target.setClientId(cid);
        push.pushMessageToSingle(message, target);
    }

    /**
     * 执行群推
     */
    public static void pushMessageToApp(AppPush appPush) {
        Gson gson = new Gson();
        String s = gson.toJson(appPush);
        IGtPush push = new IGtPush(url, appKey, masterSecret);
        // STEP4：选择通知模板
        TransmissionTemplate template = new TransmissionTemplate();
        template.setAppId(appId);
        template.setAppkey(appKey);
        template.setTransmissionContent(s);
        template.setTransmissionType(2);
        APNPayload apnPayload = getAPNPayload("+1", 1, "default", appPush);
        template.setAPNInfo(apnPayload);
        List<String> appIds = new ArrayList<String>();
        appIds.add(appId);
        AppMessage message = new AppMessage();
        message.setData(template);
        message.setAppIdList(appIds);
        message.setOffline(true);
        message.setOfflineExpireTime(1000 * 600 * 24);  // 时间单位为毫秒
        push.pushMessageToApp(message);
    }

    /**
     * 封装   APNPayload
     * @param badge 角标
     * @param contentAvailable
     * @param sound 声音
     * @param appPush   数据
     * @return
     */
    private static APNPayload getAPNPayload(String badge, int contentAvailable, String sound, AppPush appPush) {
        APNPayload apnPayload = new APNPayload();
        //设置角标
        apnPayload.setAutoBadge(badge);
        apnPayload.setContentAvailable(contentAvailable);
        //设置声音
        apnPayload.setSound(sound);
        //创建消息体
        APNPayload.DictionaryAlertMsg dictionaryAlertMsg = new APNPayload.DictionaryAlertMsg();
        dictionaryAlertMsg.setBody(appPush.getContent());
        dictionaryAlertMsg.setTitle(appPush.getTitle());
        //设置消息体
        apnPayload.setAlertMsg(dictionaryAlertMsg);
        return apnPayload;
    }

}